{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       3999.640   4003.497   4007.354   4011.211   4015.068   4018.925   \\\n",
      "编号\\波数                                                                     \n",
      "1-1         1.43   1.393860   1.430795   1.406880   1.466318   1.430019   \n",
      "1-2         1.41   1.384912   1.434742   1.398214   1.431504   1.385537   \n",
      "1-3         1.43   1.368902   1.422482   1.394730   1.433988   1.405348   \n",
      "1-4         1.47   1.420488   1.466840   1.418439   1.465973   1.444678   \n",
      "1-5         1.46   1.401973   1.431425   1.403622   1.461123   1.427679   \n",
      "\n",
      "       4022.781   4026.638   4030.495   4034.352   ...  9966.316   9970.173   \\\n",
      "编号\\波数                                              ...                         \n",
      "1-1     1.450731   1.399648   1.394664   1.327548  ...  -0.037982  -0.038196   \n",
      "1-2     1.407498   1.362763   1.378757   1.346508  ...  -0.043363  -0.043433   \n",
      "1-3     1.425170   1.370816   1.392904   1.331552  ...  -0.043633  -0.043443   \n",
      "1-4     1.468752   1.416320   1.434113   1.358064  ...  -0.042266  -0.042412   \n",
      "1-5     1.437793   1.387369   1.404795   1.335858  ...  -0.041887  -0.042047   \n",
      "\n",
      "       9974.030   9977.887   9981.744   9985.601   9989.458   9993.314   \\\n",
      "编号\\波数                                                                     \n",
      "1-1    -0.037789  -0.037316  -0.037736  -0.037983  -0.038106  -0.038109   \n",
      "1-2    -0.043117  -0.042641  -0.042982  -0.043372  -0.043620  -0.043472   \n",
      "1-3    -0.042872  -0.042789  -0.043399  -0.043538  -0.043645  -0.043552   \n",
      "1-4    -0.042182  -0.041996  -0.042311  -0.042686  -0.042857  -0.042657   \n",
      "1-5    -0.041532  -0.041236  -0.041622  -0.041751  -0.041959  -0.041850   \n",
      "\n",
      "       9997.172   10001.030  \n",
      "编号\\波数                        \n",
      "1-1    -0.038106  -0.038025  \n",
      "1-2    -0.043117  -0.043228  \n",
      "1-3    -0.043365  -0.043443  \n",
      "1-4    -0.042421  -0.042315  \n",
      "1-5    -0.041626  -0.041699  \n",
      "\n",
      "[5 rows x 1557 columns]\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import pandas as pd\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from scipy.stats import pearsonr\n",
    "import numpy as np\n",
    "import pywt\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Dense, Conv1D, LSTM, BatchNormalization, LeakyReLU, AveragePooling1D, Dropout\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.callbacks import EarlyStopping\n",
    "\n",
    "# 读取数据\n",
    "file_path = 'F:\\\\研究\\\\番泻苷在线提取数据.xlsx'\n",
    "with pd.ExcelFile(file_path) as xls:\n",
    "    ir_data = pd.read_excel(xls, '红外谱图', index_col='编号\\波数')\n",
    "    targets = pd.read_excel(xls, '番泻苷含量')[['番泻苷B']]\n",
    "\n",
    "print(ir_data.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       3999.640   4003.497   4007.354   4011.211   4015.068   4018.925   \\\n",
      "编号\\波数                                                                     \n",
      "1-1     0.059828   0.056702   0.069131   0.066483   0.062350   0.055401   \n",
      "1-2     0.059906   0.056668   0.069041   0.066570   0.062753   0.055583   \n",
      "1-3     0.059983   0.056637   0.068957   0.066654   0.063137   0.055756   \n",
      "1-4     0.060057   0.056611   0.068880   0.066736   0.063501   0.055920   \n",
      "1-5     0.060130   0.056589   0.068809   0.066815   0.063844   0.056074   \n",
      "\n",
      "       4022.781   4026.638   4030.495   4034.352   ...  9966.316   9970.173   \\\n",
      "编号\\波数                                              ...                         \n",
      "1-1     0.060027   0.054168   0.064917   0.045003  ...   0.022974   0.022965   \n",
      "1-2     0.060172   0.054333   0.064893   0.044975  ...   0.022978   0.022968   \n",
      "1-3     0.060312   0.054491   0.064871   0.044948  ...   0.022982   0.022972   \n",
      "1-4     0.060445   0.054643   0.064849   0.044922  ...   0.022987   0.022977   \n",
      "1-5     0.060572   0.054787   0.064829   0.044897  ...   0.022992   0.022981   \n",
      "\n",
      "       9974.030   9977.887   9981.744   9985.601   9989.458   9993.314   \\\n",
      "编号\\波数                                                                     \n",
      "1-1     0.022903   0.022776   0.022762   0.022651   0.022865   0.022990   \n",
      "1-2     0.022909   0.022781   0.022765   0.022656   0.022869   0.022993   \n",
      "1-3     0.022915   0.022787   0.022768   0.022661   0.022874   0.022997   \n",
      "1-4     0.022921   0.022793   0.022772   0.022667   0.022878   0.023002   \n",
      "1-5     0.022928   0.022799   0.022776   0.022672   0.022883   0.023006   \n",
      "\n",
      "       9997.172   10001.030  \n",
      "编号\\波数                        \n",
      "1-1     0.022990   0.022938  \n",
      "1-2     0.022992   0.022938  \n",
      "1-3     0.022994   0.022938  \n",
      "1-4     0.022997   0.022938  \n",
      "1-5     0.023000   0.022939  \n",
      "\n",
      "[5 rows x 1557 columns]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from scipy.signal import savgol_filter\n",
    "\n",
    "\n",
    "# 1. 基线校正\n",
    "def baseline_correction(spectra):\n",
    "    # 趋势校正：使用Savitzky-Golay滤波器来拟合趋势，然后从原始光谱中减去这个趋势\n",
    "    trend = savgol_filter(spectra, 51, 2, deriv=0, axis=0)  # 使用窗口大小为51，多项式阶数为2的S-G滤波器\n",
    "    detrended = spectra - trend\n",
    "    \n",
    "    # 位移校正：减去每列的最小值\n",
    "    offset_corrected = detrended - np.min(detrended, axis=0)\n",
    "    \n",
    "    return offset_corrected\n",
    "\n",
    "# 2. 平滑处理\n",
    "def smoothing(spectra):\n",
    "    # 使用Savitzky-Golay滤波器进行平滑处理\n",
    "    smoothed = savgol_filter(spectra, 51, 2, axis=0)  # 同样使用窗口大小为51，多项式阶数为2\n",
    "    return smoothed\n",
    "\n",
    "# 应用预处理步骤\n",
    "# 提取光谱数据\n",
    "spectra = ir_data.values\n",
    "\n",
    "# 基线校正\n",
    "spectra_corrected = baseline_correction(spectra)\n",
    "\n",
    "# 平滑处理\n",
    "spectra_smoothed = smoothing(spectra_corrected)\n",
    "\n",
    "# 将预处理后的数据转换回DataFrame\n",
    "spectra_preprocessed_df = pd.DataFrame(spectra_smoothed, index=ir_data.index, columns=ir_data.columns)\n",
    "\n",
    "# 打印预处理后的光谱数据头几行\n",
    "print(spectra_preprocessed_df.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳epochs轮数: 419\n",
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 179ms/step\n",
      "\u001b[1m11/11\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 3ms/step \n",
      "RMSEc (校正均方根误差): 9.395218230585069\n",
      "RMSEp (预测均方根误差): 13.242065782979305\n",
      "Rcal (校正集相关系数): 0.9902746168682646\n",
      "Rval (验证集相关系数): 0.9846574601588778\n",
      "RPD (相对预测偏差): 5.5689982123501505\n",
      "Training time: 94.15725445747375 seconds\n",
      "Testing time: 0.7532672882080078 seconds\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import pandas as pd\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from scipy.stats import pearsonr\n",
    "import numpy as np\n",
    "import pywt\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Dense, Conv1D, LSTM, BatchNormalization, LeakyReLU, AveragePooling1D, Dropout\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.callbacks import EarlyStopping\n",
    "\n",
    "# 读取数据\n",
    "file_path = 'F:\\\\研究\\\\番泻苷在线提取数据.xlsx'\n",
    "with pd.ExcelFile(file_path) as xls:\n",
    "    ir_data = pd.read_excel(xls, '红外谱图', index_col='编号\\波数')\n",
    "    targets = pd.read_excel(xls, '番泻苷含量')[['番泻苷B']]\n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "# 特征提取\n",
    "def extract_features(data):\n",
    "    pca = PCA(n_components=10)\n",
    "    pca_features = pca.fit_transform(data)\n",
    "    return pd.DataFrame(pca_features, columns=['PC' + str(i) for i in range(1, 11)])\n",
    "\n",
    "pca_features_df = extract_features(ir_data)\n",
    "\n",
    "# 索引对齐\n",
    "pca_features_df, targets = pca_features_df.align(targets, join='inner', axis=0)\n",
    "\n",
    "# Kennard-Stone算法实现省略，假设函数名为kennard_stone_selection\n",
    "def kennard_stone_selection(x_variables, k):\n",
    "    x_variables = np.array(x_variables)\n",
    "    original_x = x_variables\n",
    "    distance_to_average = ((x_variables - np.tile(x_variables.mean(axis=0), (x_variables.shape[0], 1))) ** 2).sum(\n",
    "        axis=1)\n",
    "    max_distance_sample_number = np.where(distance_to_average == np.max(distance_to_average))\n",
    "    max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "    selected_sample_numbers = list()\n",
    "    selected_sample_numbers.append(max_distance_sample_number)\n",
    "    remaining_sample_numbers = np.arange(0, x_variables.shape[0], 1)\n",
    "    x_variables = np.delete(x_variables, selected_sample_numbers, 0)\n",
    "    remaining_sample_numbers = np.delete(remaining_sample_numbers, selected_sample_numbers, 0)\n",
    "    for iteration in range(1, k):\n",
    "        selected_samples = original_x[selected_sample_numbers, :]\n",
    "        min_distance_to_selected_samples = list()\n",
    "        for min_distance_calculation_number in range(0, x_variables.shape[0]):\n",
    "            distance_to_selected_samples = ((selected_samples - np.tile(x_variables[min_distance_calculation_number, :],\n",
    "                                                                        (selected_samples.shape[0], 1))) ** 2).sum(\n",
    "                axis=1)\n",
    "            min_distance_to_selected_samples.append(np.min(distance_to_selected_samples))\n",
    "        max_distance_sample_number = np.where(\n",
    "            min_distance_to_selected_samples == np.max(min_distance_to_selected_samples))\n",
    "        max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "        selected_sample_numbers.append(remaining_sample_numbers[max_distance_sample_number])\n",
    "        x_variables = np.delete(x_variables, max_distance_sample_number, 0)\n",
    "        remaining_sample_numbers = np.delete(remaining_sample_numbers, max_distance_sample_number, 0)\n",
    "\n",
    "    return selected_sample_numbers, remaining_sample_numbers\n",
    "\n",
    "\n",
    "# 划分数据集\n",
    "train_indices, test_indices = kennard_stone_selection(pca_features_df.values, 336)\n",
    "X_train, X_test = pca_features_df.iloc[train_indices], pca_features_df.iloc[test_indices]\n",
    "y_train, y_test = targets.iloc[train_indices], targets.iloc[test_indices]\n",
    "\n",
    "# 调整数据形状\n",
    "X_train = np.expand_dims(X_train, axis=2)\n",
    "X_test = np.expand_dims(X_test, axis=2)\n",
    "\n",
    "# 构建模型\n",
    "def build_aclstm_model(input_shape):\n",
    "    model = Sequential([\n",
    "        Conv1D(filters=128, kernel_size=3, activation='relu', input_shape=input_shape),\n",
    "        BatchNormalization(),\n",
    "        LeakyReLU(alpha=0.01),\n",
    "        AveragePooling1D(pool_size=2),\n",
    "        Dropout(0.2),\n",
    "        Conv1D(filters=128, kernel_size=3, activation='relu'),\n",
    "        BatchNormalization(),\n",
    "        LeakyReLU(alpha=0.01),\n",
    "        AveragePooling1D(pool_size=2),\n",
    "        Dropout(0.2),\n",
    "        LSTM(128, return_sequences=True),\n",
    "        LSTM(128),\n",
    "        Dense(1)\n",
    "    ])\n",
    "    model.compile(optimizer=Adam(learning_rate=1e-3), loss='mean_squared_error')\n",
    "    return model\n",
    "\n",
    "model_aclstm = build_aclstm_model(X_train.shape[1:])\n",
    "\n",
    "# 训练模型\n",
    "start_train_time = time.time()\n",
    "early_stopping = EarlyStopping(monitor='val_loss', patience=100)\n",
    "history = model_aclstm.fit(X_train, y_train, epochs=1000, batch_size=10, validation_split=0.2, verbose=0, callbacks=[early_stopping])\n",
    "\n",
    "# 获取最佳epochs轮数\n",
    "best_epoch = early_stopping.stopped_epoch + 1  # +1 因为stopped_epoch是从0开始的\n",
    "print(f\"最佳epochs轮数: {best_epoch}\")\n",
    "\n",
    "end_train_time = time.time()\n",
    "train_time = end_train_time - start_train_time\n",
    "\n",
    "# 测试模型\n",
    "start_test_time = time.time()\n",
    "y_pred_aclstm = model_aclstm.predict(X_test)\n",
    "end_test_time = time.time()\n",
    "test_time = end_test_time - start_test_time\n",
    "\n",
    "# 计算性能指标\n",
    "def calculate_metrics(y_true, y_pred):\n",
    "    rmse = np.sqrt(mean_squared_error(y_true, y_pred))\n",
    "    r = pearsonr(y_true.ravel(), y_pred.ravel())[0]\n",
    "    return rmse, r\n",
    "\n",
    "rmsec, r_cal = calculate_metrics(y_train.values, model_aclstm.predict(X_train))\n",
    "rmsep, r_val = calculate_metrics(y_test.values, y_pred_aclstm)\n",
    "RPD = np.std(y_test.values) / rmsep\n",
    "\n",
    "# 输出性能指标\n",
    "print(f\"RMSEc (校正均方根误差): {rmsec}\\nRMSEp (预测均方根误差): {rmsep}\\nRcal (校正集相关系数): {r_cal}\\nRval (验证集相关系数): {r_val}\\nRPD (相对预测偏差): {RPD}\")\n",
    "print(f\"Training time: {train_time} seconds\")\n",
    "print(f\"Testing time: {test_time} seconds\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳epochs轮数: 150\n",
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 136ms/step\n",
      "\u001b[1m11/11\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 6ms/step\n",
      "RMSEc (校正均方根误差): 66.04208205611455\n",
      "RMSEp (预测均方根误差): 73.97938028308356\n",
      "Rcal (校正集相关系数): -0.12115985463564007\n",
      "Rval (验证集相关系数): -0.34675387701171223\n",
      "RPD (相对预测偏差): 0.996832365870706\n",
      "Training time: 38.0634331703186 seconds\n",
      "Testing time: 0.6165368556976318 seconds\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import pandas as pd\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from scipy.stats import pearsonr\n",
    "import numpy as np\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Dense, Conv1D, LSTM\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.callbacks import EarlyStopping\n",
    "\n",
    "# 读取数据\n",
    "file_path = 'F:\\\\研究\\\\番泻苷在线提取数据.xlsx'\n",
    "with pd.ExcelFile(file_path) as xls:\n",
    "    ir_data = pd.read_excel(xls, '红外谱图', index_col='编号\\波数')\n",
    "    targets = pd.read_excel(xls, '番泻苷含量')[['番泻苷B']]\n",
    "\n",
    "# 特征提取\n",
    "def extract_features(data):\n",
    "    pca = PCA(n_components=10)\n",
    "    pca_features = pca.fit_transform(data)\n",
    "    return pd.DataFrame(pca_features, columns=['PC' + str(i) for i in range(1, 11)])\n",
    "\n",
    "pca_features_df = extract_features(ir_data)\n",
    "\n",
    "# 索引对齐\n",
    "pca_features_df, targets = pca_features_df.align(targets, join='inner', axis=0)\n",
    "\n",
    "# Kennard-Stone算法实现省略，假设函数名为kennard_stone_selection\n",
    "def kennard_stone_selection(x_variables, k):\n",
    "    x_variables = np.array(x_variables)\n",
    "    original_x = x_variables\n",
    "    distance_to_average = ((x_variables - np.tile(x_variables.mean(axis=0), (x_variables.shape[0], 1))) ** 2).sum(\n",
    "        axis=1)\n",
    "    max_distance_sample_number = np.where(distance_to_average == np.max(distance_to_average))\n",
    "    max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "    selected_sample_numbers = list()\n",
    "    selected_sample_numbers.append(max_distance_sample_number)\n",
    "    remaining_sample_numbers = np.arange(0, x_variables.shape[0], 1)\n",
    "    x_variables = np.delete(x_variables, selected_sample_numbers, 0)\n",
    "    remaining_sample_numbers = np.delete(remaining_sample_numbers, selected_sample_numbers, 0)\n",
    "    for iteration in range(1, k):\n",
    "        selected_samples = original_x[selected_sample_numbers, :]\n",
    "        min_distance_to_selected_samples = list()\n",
    "        for min_distance_calculation_number in range(0, x_variables.shape[0]):\n",
    "            distance_to_selected_samples = ((selected_samples - np.tile(x_variables[min_distance_calculation_number, :],\n",
    "                                                                        (selected_samples.shape[0], 1))) ** 2).sum(\n",
    "                axis=1)\n",
    "            min_distance_to_selected_samples.append(np.min(distance_to_selected_samples))\n",
    "        max_distance_sample_number = np.where(\n",
    "            min_distance_to_selected_samples == np.max(min_distance_to_selected_samples))\n",
    "        max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "        selected_sample_numbers.append(remaining_sample_numbers[max_distance_sample_number])\n",
    "        x_variables = np.delete(x_variables, max_distance_sample_number, 0)\n",
    "        remaining_sample_numbers = np.delete(remaining_sample_numbers, max_distance_sample_number, 0)\n",
    "\n",
    "    return selected_sample_numbers, remaining_sample_numbers\n",
    "\n",
    "\n",
    "# 划分数据集\n",
    "train_indices, test_indices = kennard_stone_selection(pca_features_df.values, 336)\n",
    "X_train, X_test = pca_features_df.iloc[train_indices], pca_features_df.iloc[test_indices]\n",
    "y_train, y_test = targets.iloc[train_indices], targets.iloc[test_indices]\n",
    "\n",
    "# 调整数据形状\n",
    "X_train = np.expand_dims(X_train, axis=2)\n",
    "X_test = np.expand_dims(X_test, axis=2)\n",
    "\n",
    "# 构建CLSTM模型\n",
    "def CLSTM_model(input_shape, output_shape):\n",
    "    model = Sequential()\n",
    "    model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=input_shape))\n",
    "    model.add(LSTM(64, return_sequences=True))\n",
    "    model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))\n",
    "    model.add(LSTM(64))\n",
    "    model.add(Dense(output_shape))\n",
    "    model.compile(optimizer=Adam(learning_rate=1e-3), loss='mean_squared_error')\n",
    "    return model\n",
    "\n",
    "model_clstm = CLSTM_model(X_train.shape[1:], 1)\n",
    "\n",
    "# 训练模型\n",
    "start_train_time = time.time()\n",
    "early_stopping = EarlyStopping(monitor='val_loss', patience=100)\n",
    "history = model_clstm.fit(X_train, y_train, epochs=1000, batch_size=10, validation_split=0.2, verbose=0, callbacks=[early_stopping])\n",
    "\n",
    "# 获取最佳epochs轮数\n",
    "best_epoch = early_stopping.stopped_epoch + 1  # +1 因为stopped_epoch是从0开始的\n",
    "print(f\"最佳epochs轮数: {best_epoch}\")\n",
    "\n",
    "end_train_time = time.time()\n",
    "train_time = end_train_time - start_train_time\n",
    "\n",
    "# 测试模型\n",
    "start_test_time = time.time()\n",
    "y_pred_clstm = model_clstm.predict(X_test)\n",
    "end_test_time = time.time()\n",
    "test_time = end_test_time - start_test_time\n",
    "\n",
    "# 计算性能指标\n",
    "def calculate_metrics(y_true, y_pred):\n",
    "    rmse = np.sqrt(mean_squared_error(y_true, y_pred))\n",
    "    r = pearsonr(y_true.ravel(), y_pred.ravel())[0]\n",
    "    return rmse, r\n",
    "\n",
    "rmsec, r_cal = calculate_metrics(y_train.values, model_clstm.predict(X_train))\n",
    "rmsep, r_val = calculate_metrics(y_test.values, y_pred_clstm)\n",
    "RPD = np.std(y_test.values) / rmsep\n",
    "\n",
    "# 输出性能指标\n",
    "print(f\"RMSEc (校正均方根误差): {rmsec}\\nRMSEp (预测均方根误差): {rmsep}\\nRcal (校正集相关系数): {r_cal}\\nRval (验证集相关系数): {r_val}\\nRPD (相对预测偏差): {RPD}\")\n",
    "print(f\"Training time: {train_time} seconds\")\n",
    "print(f\"Testing time: {test_time} seconds\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳epochs轮数: 577\n",
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 40ms/step\n",
      "\u001b[1m11/11\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 3ms/step \n",
      "RMSEc (校正均方根误差): 9.958781908815755\n",
      "RMSEp (预测均方根误差): 19.395066061267425\n",
      "Rcal (校正集相关系数): 0.9887157899126443\n",
      "Rval (验证集相关系数): 0.9702553926648896\n",
      "RPD (相对预测偏差): 3.8022577721715565\n",
      "Training time: 84.39926505088806 seconds\n",
      "Testing time: 0.23558521270751953 seconds\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import pandas as pd\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from scipy.stats import pearsonr\n",
    "import numpy as np\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.callbacks import EarlyStopping\n",
    "from tensorflow.keras.layers import Dense, Conv1D, LSTM, MaxPooling1D  # 导入 MaxPooling1D\n",
    "\n",
    "# 读取数据\n",
    "file_path = 'F:\\\\研究\\\\番泻苷在线提取数据.xlsx'\n",
    "with pd.ExcelFile(file_path) as xls:\n",
    "    ir_data = pd.read_excel(xls, '红外谱图', index_col='编号\\波数')\n",
    "    targets = pd.read_excel(xls, '番泻苷含量')[['番泻苷B']]\n",
    "\n",
    "# 特征提取\n",
    "def extract_features(data):\n",
    "    pca = PCA(n_components=10)\n",
    "    pca_features = pca.fit_transform(data)\n",
    "    return pd.DataFrame(pca_features, columns=['PC' + str(i) for i in range(1, 11)])\n",
    "\n",
    "pca_features_df = extract_features(ir_data)\n",
    "\n",
    "# 索引对齐\n",
    "pca_features_df, targets = pca_features_df.align(targets, join='inner', axis=0)\n",
    "\n",
    "# Kennard-Stone算法实现省略，假设函数名为kennard_stone_selection\n",
    "def kennard_stone_selection(x_variables, k):\n",
    "    x_variables = np.array(x_variables)\n",
    "    original_x = x_variables\n",
    "    distance_to_average = ((x_variables - np.tile(x_variables.mean(axis=0), (x_variables.shape[0], 1))) ** 2).sum(\n",
    "        axis=1)\n",
    "    max_distance_sample_number = np.where(distance_to_average == np.max(distance_to_average))\n",
    "    max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "    selected_sample_numbers = list()\n",
    "    selected_sample_numbers.append(max_distance_sample_number)\n",
    "    remaining_sample_numbers = np.arange(0, x_variables.shape[0], 1)\n",
    "    x_variables = np.delete(x_variables, selected_sample_numbers, 0)\n",
    "    remaining_sample_numbers = np.delete(remaining_sample_numbers, selected_sample_numbers, 0)\n",
    "    for iteration in range(1, k):\n",
    "        selected_samples = original_x[selected_sample_numbers, :]\n",
    "        min_distance_to_selected_samples = list()\n",
    "        for min_distance_calculation_number in range(0, x_variables.shape[0]):\n",
    "            distance_to_selected_samples = ((selected_samples - np.tile(x_variables[min_distance_calculation_number, :],\n",
    "                                                                        (selected_samples.shape[0], 1))) ** 2).sum(\n",
    "                axis=1)\n",
    "            min_distance_to_selected_samples.append(np.min(distance_to_selected_samples))\n",
    "        max_distance_sample_number = np.where(\n",
    "            min_distance_to_selected_samples == np.max(min_distance_to_selected_samples))\n",
    "        max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "        selected_sample_numbers.append(remaining_sample_numbers[max_distance_sample_number])\n",
    "        x_variables = np.delete(x_variables, max_distance_sample_number, 0)\n",
    "        remaining_sample_numbers = np.delete(remaining_sample_numbers, max_distance_sample_number, 0)\n",
    "\n",
    "    return selected_sample_numbers, remaining_sample_numbers\n",
    "\n",
    "\n",
    "# 划分数据集\n",
    "train_indices, test_indices = kennard_stone_selection(pca_features_df.values, 336)\n",
    "X_train, X_test = pca_features_df.iloc[train_indices], pca_features_df.iloc[test_indices]\n",
    "y_train, y_test = targets.iloc[train_indices], targets.iloc[test_indices]\n",
    "\n",
    "# 调整数据形状\n",
    "X_train = np.expand_dims(X_train, axis=2)\n",
    "X_test = np.expand_dims(X_test, axis=2)\n",
    "\n",
    "# 构建CNN模型\n",
    "def CNN_model(input_shape, output_shape):\n",
    "    model = Sequential()\n",
    "    model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=input_shape))\n",
    "    model.add(MaxPooling1D(pool_size=2))  # 使用导入的 MaxPooling1D\n",
    "    model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))\n",
    "    model.add(MaxPooling1D(pool_size=2))\n",
    "    model.add(Flatten())\n",
    "    model.add(Dense(64, activation='relu'))\n",
    "    model.add(Dense(output_shape))\n",
    "    model.compile(optimizer=Adam(learning_rate=1e-3), loss='mean_squared_error')\n",
    "    return model\n",
    "\n",
    "model_clstm = CNN_model(X_train.shape[1:], 1)\n",
    "\n",
    "# 训练模型\n",
    "start_train_time = time.time()\n",
    "early_stopping = EarlyStopping(monitor='val_loss', patience=100)\n",
    "history = model_clstm.fit(X_train, y_train, epochs=1000, batch_size=10, validation_split=0.2, verbose=0, callbacks=[early_stopping])\n",
    "\n",
    "# 获取最佳epochs轮数\n",
    "best_epoch = early_stopping.stopped_epoch + 1  # +1 因为stopped_epoch是从0开始的\n",
    "print(f\"最佳epochs轮数: {best_epoch}\")\n",
    "\n",
    "end_train_time = time.time()\n",
    "train_time = end_train_time - start_train_time\n",
    "\n",
    "# 测试模型\n",
    "start_test_time = time.time()\n",
    "y_pred_clstm = model_clstm.predict(X_test)\n",
    "end_test_time = time.time()\n",
    "test_time = end_test_time - start_test_time\n",
    "\n",
    "# 计算性能指标\n",
    "def calculate_metrics(y_true, y_pred):\n",
    "    rmse = np.sqrt(mean_squared_error(y_true, y_pred))\n",
    "    r = pearsonr(y_true.ravel(), y_pred.ravel())[0]\n",
    "    return rmse, r\n",
    "\n",
    "rmsec, r_cal = calculate_metrics(y_train.values, model_clstm.predict(X_train))\n",
    "rmsep, r_val = calculate_metrics(y_test.values, y_pred_clstm)\n",
    "RPD = np.std(y_test.values) / rmsep\n",
    "\n",
    "# 输出性能指标\n",
    "print(f\"RMSEc (校正均方根误差): {rmsec}\\nRMSEp (预测均方根误差): {rmsep}\\nRcal (校正集相关系数): {r_cal}\\nRval (验证集相关系数): {r_val}\\nRPD (相对预测偏差): {RPD}\")\n",
    "print(f\"Training time: {train_time} seconds\")\n",
    "print(f\"Testing time: {test_time} seconds\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\rnn\\rnn.py:204: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(**kwargs)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳epochs轮数: 826\n",
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 144ms/step\n",
      "\u001b[1m11/11\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 2ms/step \n",
      "RMSEc (校正均方根误差): 4.697960757718751\n",
      "RMSEp (预测均方根误差): 7.419475728398666\n",
      "Rcal (校正集相关系数): 0.9974726860511853\n",
      "Rval (验证集相关系数): 0.9956030064648977\n",
      "RPD (相对预测偏差): 9.93938700964672\n",
      "Training time: 122.09112524986267 seconds\n",
      "Testing time: 0.6043834686279297 seconds\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import pandas as pd\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from scipy.stats import pearsonr\n",
    "import numpy as np\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.callbacks import EarlyStopping\n",
    "from tensorflow.keras.layers import Dense, Conv1D, LSTM, MaxPooling1D  # 导入 MaxPooling1D\n",
    "from tensorflow.keras.layers import LSTM, Flatten  # 导入 Flatten 和 LSTM\n",
    "\n",
    "# 读取数据\n",
    "file_path = 'F:\\\\研究\\\\番泻苷在线提取数据.xlsx'\n",
    "with pd.ExcelFile(file_path) as xls:\n",
    "    ir_data = pd.read_excel(xls, '红外谱图', index_col='编号\\波数')\n",
    "    targets = pd.read_excel(xls, '番泻苷含量')[['番泻苷B']]\n",
    "\n",
    "# 特征提取\n",
    "def extract_features(data):\n",
    "    pca = PCA(n_components=10)\n",
    "    pca_features = pca.fit_transform(data)\n",
    "    return pd.DataFrame(pca_features, columns=['PC' + str(i) for i in range(1, 11)])\n",
    "\n",
    "pca_features_df = extract_features(ir_data)\n",
    "\n",
    "# 索引对齐\n",
    "pca_features_df, targets = pca_features_df.align(targets, join='inner', axis=0)\n",
    "\n",
    "# Kennard-Stone算法实现省略，假设函数名为kennard_stone_selection\n",
    "def kennard_stone_selection(x_variables, k):\n",
    "    x_variables = np.array(x_variables)\n",
    "    original_x = x_variables\n",
    "    distance_to_average = ((x_variables - np.tile(x_variables.mean(axis=0), (x_variables.shape[0], 1))) ** 2).sum(\n",
    "        axis=1)\n",
    "    max_distance_sample_number = np.where(distance_to_average == np.max(distance_to_average))\n",
    "    max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "    selected_sample_numbers = list()\n",
    "    selected_sample_numbers.append(max_distance_sample_number)\n",
    "    remaining_sample_numbers = np.arange(0, x_variables.shape[0], 1)\n",
    "    x_variables = np.delete(x_variables, selected_sample_numbers, 0)\n",
    "    remaining_sample_numbers = np.delete(remaining_sample_numbers, selected_sample_numbers, 0)\n",
    "    for iteration in range(1, k):\n",
    "        selected_samples = original_x[selected_sample_numbers, :]\n",
    "        min_distance_to_selected_samples = list()\n",
    "        for min_distance_calculation_number in range(0, x_variables.shape[0]):\n",
    "            distance_to_selected_samples = ((selected_samples - np.tile(x_variables[min_distance_calculation_number, :],\n",
    "                                                                        (selected_samples.shape[0], 1))) ** 2).sum(\n",
    "                axis=1)\n",
    "            min_distance_to_selected_samples.append(np.min(distance_to_selected_samples))\n",
    "        max_distance_sample_number = np.where(\n",
    "            min_distance_to_selected_samples == np.max(min_distance_to_selected_samples))\n",
    "        max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "        selected_sample_numbers.append(remaining_sample_numbers[max_distance_sample_number])\n",
    "        x_variables = np.delete(x_variables, max_distance_sample_number, 0)\n",
    "        remaining_sample_numbers = np.delete(remaining_sample_numbers, max_distance_sample_number, 0)\n",
    "\n",
    "    return selected_sample_numbers, remaining_sample_numbers\n",
    "\n",
    "\n",
    "# 划分数据集\n",
    "train_indices, test_indices = kennard_stone_selection(pca_features_df.values, 336)\n",
    "X_train, X_test = pca_features_df.iloc[train_indices], pca_features_df.iloc[test_indices]\n",
    "y_train, y_test = targets.iloc[train_indices], targets.iloc[test_indices]\n",
    "\n",
    "# 调整数据形状\n",
    "X_train = np.expand_dims(X_train, axis=2)\n",
    "X_test = np.expand_dims(X_test, axis=2)\n",
    "\n",
    "# 构建LSTM模型\n",
    "def LSTM_model(input_shape, output_shape):\n",
    "    model = Sequential()\n",
    "    model.add(LSTM(128, input_shape=(1, 10), return_sequences=True))  # 修正 input_shape\n",
    "    model.add(LSTM(128))\n",
    "    model.add(Dense(output_shape))\n",
    "    model.compile(optimizer=Adam(learning_rate=1e-3), loss='mean_squared_error')\n",
    "    return model\n",
    "\n",
    "\n",
    "# 调整数据形状以匹配LSTM输入要求\n",
    "# LSTM需要三维输入，因此需要添加一个时间步长维度\n",
    "X_train = np.expand_dims(X_train, axis=1)  # 现在形状是(samples, 1, features)\n",
    "X_test = np.expand_dims(X_test, axis=1)    # 现在形状是(samples, 1, features)\n",
    "\n",
    "# 创建LSTM模型\n",
    "model_lstm = LSTM_model(X_train.shape[1:], 1)\n",
    "\n",
    "start_train_time = time.time()\n",
    "early_stopping = EarlyStopping(monitor='val_loss', patience=100)\n",
    "history = model_lstm.fit(X_train, y_train, epochs=1000, batch_size=10, validation_split=0.2, verbose=0, callbacks=[early_stopping])\n",
    "\n",
    "\n",
    "# 获取最佳epochs轮数\n",
    "best_epoch = early_stopping.stopped_epoch + 1  # +1 因为stopped_epoch是从0开始的\n",
    "print(f\"最佳epochs轮数: {best_epoch}\")\n",
    "\n",
    "end_train_time = time.time()\n",
    "train_time = end_train_time - start_train_time\n",
    "\n",
    "# 测试模型\n",
    "start_test_time = time.time()\n",
    "y_pred_lstm = model_lstm.predict(X_test)\n",
    "end_test_time = time.time()\n",
    "test_time = end_test_time - start_test_time\n",
    "\n",
    "# 计算性能指标\n",
    "def calculate_metrics(y_true, y_pred):\n",
    "    rmse = np.sqrt(mean_squared_error(y_true, y_pred))\n",
    "    r = pearsonr(y_true.ravel(), y_pred.ravel())[0]\n",
    "    return rmse, r\n",
    "\n",
    "rmsec, r_cal = calculate_metrics(y_train.values, model_lstm.predict(X_train))\n",
    "rmsep, r_val = calculate_metrics(y_test.values, y_pred_lstm)\n",
    "RPD = np.std(y_test.values) / rmsep\n",
    "\n",
    "# 输出性能指标\n",
    "print(f\"RMSEc (校正均方根误差): {rmsec}\\nRMSEp (预测均方根误差): {rmsep}\\nRcal (校正集相关系数): {r_cal}\\nRval (验证集相关系数): {r_val}\\nRPD (相对预测偏差): {RPD}\")\n",
    "print(f\"Training time: {train_time} seconds\")\n",
    "print(f\"Testing time: {test_time} seconds\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\rnn\\rnn.py:204: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(**kwargs)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳epochs轮数: 789\n",
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 117ms/step\n",
      "\u001b[1m11/11\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 2ms/step \n",
      "RMSEc (校正均方根误差): 4.131258488054218\n",
      "RMSEp (预测均方根误差): 7.00219860672145\n",
      "Rcal (校正集相关系数): 0.9980495071174587\n",
      "Rval (验证集相关系数): 0.9958626721575506\n",
      "RPD (相对预测偏差): 10.531697944477976\n",
      "Training time: 103.46757221221924 seconds\n",
      "Testing time: 0.5075550079345703 seconds\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import pandas as pd\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from scipy.stats import pearsonr\n",
    "import numpy as np\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.callbacks import EarlyStopping\n",
    "from tensorflow.keras.layers import Dense, Conv1D, LSTM, MaxPooling1D  # 导入 MaxPooling1D\n",
    "from tensorflow.keras.layers import LSTM, Flatten  # 导入 Flatten 和 LSTM\n",
    "\n",
    "# 读取数据\n",
    "file_path = 'F:\\\\研究\\\\番泻苷在线提取数据.xlsx'\n",
    "with pd.ExcelFile(file_path) as xls:\n",
    "    ir_data = pd.read_excel(xls, '红外谱图', index_col='编号\\波数')\n",
    "    targets = pd.read_excel(xls, '番泻苷含量')[['番泻苷B']]\n",
    "\n",
    "# 特征提取\n",
    "def extract_features(data):\n",
    "    pca = PCA(n_components=10)\n",
    "    pca_features = pca.fit_transform(data)\n",
    "    return pd.DataFrame(pca_features, columns=['PC' + str(i) for i in range(1, 11)])\n",
    "\n",
    "pca_features_df = extract_features(ir_data)\n",
    "\n",
    "# 索引对齐\n",
    "pca_features_df, targets = pca_features_df.align(targets, join='inner', axis=0)\n",
    "\n",
    "# Kennard-Stone算法实现省略，假设函数名为kennard_stone_selection\n",
    "def kennard_stone_selection(x_variables, k):\n",
    "    x_variables = np.array(x_variables)\n",
    "    original_x = x_variables\n",
    "    distance_to_average = ((x_variables - np.tile(x_variables.mean(axis=0), (x_variables.shape[0], 1))) ** 2).sum(\n",
    "        axis=1)\n",
    "    max_distance_sample_number = np.where(distance_to_average == np.max(distance_to_average))\n",
    "    max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "    selected_sample_numbers = list()\n",
    "    selected_sample_numbers.append(max_distance_sample_number)\n",
    "    remaining_sample_numbers = np.arange(0, x_variables.shape[0], 1)\n",
    "    x_variables = np.delete(x_variables, selected_sample_numbers, 0)\n",
    "    remaining_sample_numbers = np.delete(remaining_sample_numbers, selected_sample_numbers, 0)\n",
    "    for iteration in range(1, k):\n",
    "        selected_samples = original_x[selected_sample_numbers, :]\n",
    "        min_distance_to_selected_samples = list()\n",
    "        for min_distance_calculation_number in range(0, x_variables.shape[0]):\n",
    "            distance_to_selected_samples = ((selected_samples - np.tile(x_variables[min_distance_calculation_number, :],\n",
    "                                                                        (selected_samples.shape[0], 1))) ** 2).sum(\n",
    "                axis=1)\n",
    "            min_distance_to_selected_samples.append(np.min(distance_to_selected_samples))\n",
    "        max_distance_sample_number = np.where(\n",
    "            min_distance_to_selected_samples == np.max(min_distance_to_selected_samples))\n",
    "        max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "        selected_sample_numbers.append(remaining_sample_numbers[max_distance_sample_number])\n",
    "        x_variables = np.delete(x_variables, max_distance_sample_number, 0)\n",
    "        remaining_sample_numbers = np.delete(remaining_sample_numbers, max_distance_sample_number, 0)\n",
    "\n",
    "    return selected_sample_numbers, remaining_sample_numbers\n",
    "\n",
    "\n",
    "# 划分数据集\n",
    "train_indices, test_indices = kennard_stone_selection(pca_features_df.values, 336)\n",
    "X_train, X_test = pca_features_df.iloc[train_indices], pca_features_df.iloc[test_indices]\n",
    "y_train, y_test = targets.iloc[train_indices], targets.iloc[test_indices]\n",
    "\n",
    "# 调整数据形状\n",
    "X_train = np.expand_dims(X_train, axis=2)\n",
    "X_test = np.expand_dims(X_test, axis=2)\n",
    "\n",
    "# 构建LSTM模型\n",
    "def LSTM_model(input_shape, output_shape):\n",
    "    model = Sequential()\n",
    "    model.add(LSTM(128, input_shape=(1, 10), return_sequences=True))  # 修正 input_shape\n",
    "    model.add(LSTM(128))\n",
    "    model.add(Dense(output_shape))\n",
    "    model.compile(optimizer=Adam(learning_rate=1e-3), loss='mean_squared_error')\n",
    "    return model\n",
    "\n",
    "\n",
    "# 调整数据形状以匹配LSTM输入要求\n",
    "# LSTM需要三维输入，因此需要添加一个时间步长维度\n",
    "X_train = np.expand_dims(X_train, axis=1)  # 现在形状是(samples, 1, features)\n",
    "X_test = np.expand_dims(X_test, axis=1)    # 现在形状是(samples, 1, features)\n",
    "\n",
    "# 创建LSTM模型\n",
    "model_lstm = LSTM_model(X_train.shape[1:], 1)\n",
    "\n",
    "start_train_time = time.time()\n",
    "early_stopping = EarlyStopping(monitor='val_loss', patience=100)\n",
    "history = model_lstm.fit(X_train, y_train, epochs=1000, batch_size=10, validation_split=0.2, verbose=0, callbacks=[early_stopping])\n",
    "\n",
    "\n",
    "# 获取最佳epochs轮数\n",
    "best_epoch = early_stopping.stopped_epoch + 1  # +1 因为stopped_epoch是从0开始的\n",
    "print(f\"最佳epochs轮数: {best_epoch}\")\n",
    "\n",
    "end_train_time = time.time()\n",
    "train_time = end_train_time - start_train_time\n",
    "\n",
    "# 测试模型\n",
    "start_test_time = time.time()\n",
    "y_pred_lstm = model_lstm.predict(X_test)\n",
    "end_test_time = time.time()\n",
    "test_time = end_test_time - start_test_time\n",
    "\n",
    "# 计算性能指标\n",
    "def calculate_metrics(y_true, y_pred):\n",
    "    rmse = np.sqrt(mean_squared_error(y_true, y_pred))\n",
    "    r = pearsonr(y_true.ravel(), y_pred.ravel())[0]\n",
    "    return rmse, r\n",
    "\n",
    "rmsec, r_cal = calculate_metrics(y_train.values, model_lstm.predict(X_train))\n",
    "rmsep, r_val = calculate_metrics(y_test.values, y_pred_lstm)\n",
    "RPD = np.std(y_test.values) / rmsep\n",
    "\n",
    "# 输出性能指标\n",
    "print(f\"RMSEc (校正均方根误差): {rmsec}\\nRMSEp (预测均方根误差): {rmsep}\\nRcal (校正集相关系数): {r_cal}\\nRval (验证集相关系数): {r_val}\\nRPD (相对预测偏差): {RPD}\")\n",
    "print(f\"Training time: {train_time} seconds\")\n",
    "print(f\"Testing time: {test_time} seconds\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\rnn\\rnn.py:204: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(**kwargs)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳epochs轮数: 280\n",
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 59ms/step\n",
      "\u001b[1m11/11\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 2ms/step \n",
      "RMSEc (校正均方根误差): 11.852802192924456\n",
      "RMSEp (预测均方根误差): 30.946147827477706\n",
      "Rcal (校正集相关系数): 0.9840511399172862\n",
      "Rval (验证集相关系数): 0.9102715165617803\n",
      "RPD (相对预测偏差): 2.3830119691910454\n",
      "Training time: 34.134602308273315 seconds\n",
      "Testing time: 0.2759134769439697 seconds\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import pandas as pd\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from scipy.stats import pearsonr\n",
    "import numpy as np\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.callbacks import EarlyStopping\n",
    "from tensorflow.keras.layers import Dense, Conv1D, LSTM, MaxPooling1D  # 导入 MaxPooling1D\n",
    "from tensorflow.keras.layers import LSTM, Flatten  # 导入 Flatten 和 LSTM\n",
    "from tensorflow.keras.layers import SimpleRNN\n",
    "\n",
    "# 读取数据\n",
    "file_path = 'F:\\\\研究\\\\番泻苷在线提取数据.xlsx'\n",
    "with pd.ExcelFile(file_path) as xls:\n",
    "    ir_data = pd.read_excel(xls, '红外谱图', index_col='编号\\波数')\n",
    "    targets = pd.read_excel(xls, '番泻苷含量')[['番泻苷B']]\n",
    "\n",
    "# 特征提取\n",
    "def extract_features(data):\n",
    "    pca = PCA(n_components=10)\n",
    "    pca_features = pca.fit_transform(data)\n",
    "    return pd.DataFrame(pca_features, columns=['PC' + str(i) for i in range(1, 11)])\n",
    "\n",
    "pca_features_df = extract_features(ir_data)\n",
    "\n",
    "# 索引对齐\n",
    "pca_features_df, targets = pca_features_df.align(targets, join='inner', axis=0)\n",
    "\n",
    "# Kennard-Stone算法实现省略，假设函数名为kennard_stone_selection\n",
    "def kennard_stone_selection(x_variables, k):\n",
    "    x_variables = np.array(x_variables)\n",
    "    original_x = x_variables\n",
    "    distance_to_average = ((x_variables - np.tile(x_variables.mean(axis=0), (x_variables.shape[0], 1))) ** 2).sum(\n",
    "        axis=1)\n",
    "    max_distance_sample_number = np.where(distance_to_average == np.max(distance_to_average))\n",
    "    max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "    selected_sample_numbers = list()\n",
    "    selected_sample_numbers.append(max_distance_sample_number)\n",
    "    remaining_sample_numbers = np.arange(0, x_variables.shape[0], 1)\n",
    "    x_variables = np.delete(x_variables, selected_sample_numbers, 0)\n",
    "    remaining_sample_numbers = np.delete(remaining_sample_numbers, selected_sample_numbers, 0)\n",
    "    for iteration in range(1, k):\n",
    "        selected_samples = original_x[selected_sample_numbers, :]\n",
    "        min_distance_to_selected_samples = list()\n",
    "        for min_distance_calculation_number in range(0, x_variables.shape[0]):\n",
    "            distance_to_selected_samples = ((selected_samples - np.tile(x_variables[min_distance_calculation_number, :],\n",
    "                                                                        (selected_samples.shape[0], 1))) ** 2).sum(\n",
    "                axis=1)\n",
    "            min_distance_to_selected_samples.append(np.min(distance_to_selected_samples))\n",
    "        max_distance_sample_number = np.where(\n",
    "            min_distance_to_selected_samples == np.max(min_distance_to_selected_samples))\n",
    "        max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "        selected_sample_numbers.append(remaining_sample_numbers[max_distance_sample_number])\n",
    "        x_variables = np.delete(x_variables, max_distance_sample_number, 0)\n",
    "        remaining_sample_numbers = np.delete(remaining_sample_numbers, max_distance_sample_number, 0)\n",
    "\n",
    "    return selected_sample_numbers, remaining_sample_numbers\n",
    "\n",
    "\n",
    "# 划分数据集\n",
    "train_indices, test_indices = kennard_stone_selection(pca_features_df.values, 336)\n",
    "X_train, X_test = pca_features_df.iloc[train_indices], pca_features_df.iloc[test_indices]\n",
    "y_train, y_test = targets.iloc[train_indices], targets.iloc[test_indices]\n",
    "\n",
    "# 调整数据形状\n",
    "X_train = np.expand_dims(X_train, axis=2)\n",
    "X_test = np.expand_dims(X_test, axis=2)\n",
    "\n",
    "# 构建RNN模型\n",
    "def RNN_model(input_shape, output_shape):\n",
    "    model = Sequential()\n",
    "    # 对于 SimpleRNN，需要指定时间步长和特征数\n",
    "    # 如果您的时间步长是可变的，可以设置为 None\n",
    "    # 假设 input_shape 是 (samples, timesteps, features)\n",
    "    model.add(SimpleRNN(128, input_shape=input_shape, activation='relu'))\n",
    "    model.add(Dense(output_shape))\n",
    "    model.compile(optimizer=Adam(learning_rate=1e-3), loss='mean_squared_error')\n",
    "    return model\n",
    "\n",
    "\n",
    "# 创建RNN模型\n",
    "model_rnn = RNN_model(X_train.shape[1:], 1)\n",
    "\n",
    "# 训练RNN模型\n",
    "start_train_time = time.time()\n",
    "early_stopping = EarlyStopping(monitor='val_loss', patience=100)\n",
    "history = model_rnn.fit(X_train, y_train, epochs=1000, batch_size=10, validation_split=0.2, verbose=0, callbacks=[early_stopping])\n",
    "\n",
    "# 获取最佳epochs轮数\n",
    "best_epoch = early_stopping.stopped_epoch + 1  # +1 因为stopped_epoch是从0开始的\n",
    "print(f\"最佳epochs轮数: {best_epoch}\")\n",
    "\n",
    "end_train_time = time.time()\n",
    "train_time = end_train_time - start_train_time\n",
    "\n",
    "# 测试模型\n",
    "start_test_time = time.time()\n",
    "y_pred_rnn = model_rnn.predict(X_test)\n",
    "end_test_time = time.time()\n",
    "test_time = end_test_time - start_test_time\n",
    "\n",
    "# 计算性能指标\n",
    "rmsec, r_cal = calculate_metrics(y_train.values, model_rnn.predict(X_train))\n",
    "rmsep, r_val = calculate_metrics(y_test.values, y_pred_rnn)\n",
    "RPD = np.std(y_test.values) / rmsep\n",
    "\n",
    "# 输出性能指标\n",
    "print(f\"RMSEc (校正均方根误差): {rmsec}\\nRMSEp (预测均方根误差): {rmsep}\\nRcal (校正集相关系数): {r_cal}\\nRval (验证集相关系数): {r_val}\\nRPD (相对预测偏差): {RPD}\")\n",
    "print(f\"Training time: {train_time} seconds\")\n",
    "print(f\"Testing time: {test_time} seconds\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Epoch 393: ReduceLROnPlateau reducing learning rate to 0.00010000000474974513.\n",
      "最佳epochs轮数: 393\n",
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 159ms/step\n",
      "\u001b[1m11/11\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 3ms/step \n",
      "RMSEc (校正均方根误差): 8.383853292746128\n",
      "RMSEp (预测均方根误差): 17.74151961529673\n",
      "Rcal (校正集相关系数): 0.9923436133486772\n",
      "Rval (验证集相关系数): 0.9723392855335943\n",
      "RPD (相对预测偏差): 4.156636087116906\n",
      "Training time: 82.41890001296997 seconds\n",
      "Testing time: 0.7078166007995605 seconds\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import pandas as pd\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from scipy.stats import pearsonr\n",
    "import numpy as np\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Dense, Conv1D, LSTM, BatchNormalization, LeakyReLU, AveragePooling1D, Dropout\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\n",
    "\n",
    "# 读取数据\n",
    "file_path = 'F:\\\\研究\\\\番泻苷在线提取数据.xlsx'\n",
    "with pd.ExcelFile(file_path) as xls:\n",
    "    ir_data = pd.read_excel(xls, '红外谱图', index_col='编号\\波数')\n",
    "    targets = pd.read_excel(xls, '番泻苷含量')[['番泻苷B']]\n",
    "\n",
    "# 特征提取\n",
    "def extract_features(data):\n",
    "    pca = PCA(n_components=10)\n",
    "    pca_features = pca.fit_transform(data)\n",
    "    return pd.DataFrame(pca_features, columns=['PC' + str(i) for i in range(1, 11)])\n",
    "\n",
    "pca_features_df = extract_features(ir_data)\n",
    "\n",
    "# 索引对齐\n",
    "pca_features_df, targets = pca_features_df.align(targets, join='inner', axis=0)\n",
    "\n",
    "# Kennard-Stone算法实现省略，假设函数名为kennard_stone_selection\n",
    "def kennard_stone_selection(x_variables, k):\n",
    "    x_variables = np.array(x_variables)\n",
    "    original_x = x_variables\n",
    "    distance_to_average = ((x_variables - np.tile(x_variables.mean(axis=0), (x_variables.shape[0], 1))) ** 2).sum(\n",
    "        axis=1)\n",
    "    max_distance_sample_number = np.where(distance_to_average == np.max(distance_to_average))\n",
    "    max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "    selected_sample_numbers = list()\n",
    "    selected_sample_numbers.append(max_distance_sample_number)\n",
    "    remaining_sample_numbers = np.arange(0, x_variables.shape[0], 1)\n",
    "    x_variables = np.delete(x_variables, selected_sample_numbers, 0)\n",
    "    remaining_sample_numbers = np.delete(remaining_sample_numbers, selected_sample_numbers, 0)\n",
    "    for iteration in range(1, k):\n",
    "        selected_samples = original_x[selected_sample_numbers, :]\n",
    "        min_distance_to_selected_samples = list()\n",
    "        for min_distance_calculation_number in range(0, x_variables.shape[0]):\n",
    "            distance_to_selected_samples = ((selected_samples - np.tile(x_variables[min_distance_calculation_number, :],\n",
    "                                                                        (selected_samples.shape[0], 1))) ** 2).sum(\n",
    "                axis=1)\n",
    "            min_distance_to_selected_samples.append(np.min(distance_to_selected_samples))\n",
    "        max_distance_sample_number = np.where(\n",
    "            min_distance_to_selected_samples == np.max(min_distance_to_selected_samples))\n",
    "        max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "        selected_sample_numbers.append(remaining_sample_numbers[max_distance_sample_number])\n",
    "        x_variables = np.delete(x_variables, max_distance_sample_number, 0)\n",
    "        remaining_sample_numbers = np.delete(remaining_sample_numbers, max_distance_sample_number, 0)\n",
    "\n",
    "    return selected_sample_numbers, remaining_sample_numbers\n",
    "\n",
    "\n",
    "# 划分数据集\n",
    "train_indices, test_indices = kennard_stone_selection(pca_features_df.values, 336)\n",
    "X_train, X_test = pca_features_df.iloc[train_indices], pca_features_df.iloc[test_indices]\n",
    "y_train, y_test = targets.iloc[train_indices], targets.iloc[test_indices]\n",
    "\n",
    "# 调整数据形状\n",
    "X_train = np.expand_dims(X_train, axis=2)\n",
    "X_test = np.expand_dims(X_test, axis=2)\n",
    "\n",
    "# 构建模型\n",
    "def build_aclstm_model(input_shape):\n",
    "    model = Sequential([\n",
    "        Conv1D(filters=128, kernel_size=3, activation='relu', input_shape=input_shape),\n",
    "        BatchNormalization(),\n",
    "        LeakyReLU(alpha=0.01),\n",
    "        AveragePooling1D(pool_size=2),\n",
    "        Dropout(0.2),\n",
    "        Conv1D(filters=128, kernel_size=3, activation='relu'),\n",
    "        BatchNormalization(),\n",
    "        LeakyReLU(alpha=0.01),\n",
    "        AveragePooling1D(pool_size=2),\n",
    "        Dropout(0.2),\n",
    "        LSTM(128, return_sequences=True),\n",
    "        LSTM(128),\n",
    "        Dense(1)\n",
    "    ])\n",
    "    model.compile(optimizer=Adam(learning_rate=1e-3), loss='mean_squared_error')\n",
    "    return model\n",
    "\n",
    "model_aclstm = build_aclstm_model(X_train.shape[1:])\n",
    "\n",
    "# 设置早停和学习率调整策略\n",
    "early_stopping = EarlyStopping(monitor='val_loss', patience=100, restore_best_weights=True)\n",
    "reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=100, min_lr=1e-5, verbose=1)\n",
    "\n",
    "# 训练模型\n",
    "start_train_time = time.time()\n",
    "history = model_aclstm.fit(X_train, y_train, epochs=1000, batch_size=10, validation_split=0.2, verbose=0, callbacks=[early_stopping, reduce_lr])\n",
    "\n",
    "# 获取最佳epochs轮数\n",
    "best_epoch = early_stopping.stopped_epoch + 1  # +1 因为stopped_epoch是从0开始的\n",
    "print(f\"最佳epochs轮数: {best_epoch}\")\n",
    "\n",
    "end_train_time = time.time()\n",
    "train_time = end_train_time - start_train_time\n",
    "\n",
    "# 测试模型\n",
    "start_test_time = time.time()\n",
    "y_pred_aclstm = model_aclstm.predict(X_test)\n",
    "end_test_time = time.time()\n",
    "test_time = end_test_time - start_test_time\n",
    "\n",
    "# 计算性能指标\n",
    "def calculate_metrics(y_true, y_pred):\n",
    "    rmse = np.sqrt(mean_squared_error(y_true, y_pred))\n",
    "    r = pearsonr(y_true.ravel(), y_pred.ravel())[0]\n",
    "    return rmse, r\n",
    "\n",
    "rmsec, r_cal = calculate_metrics(y_train.values, model_aclstm.predict(X_train))\n",
    "rmsep, r_val = calculate_metrics(y_test.values, y_pred_aclstm)\n",
    "RPD = np.std(y_test.values) / rmsep\n",
    "\n",
    "# 输出性能指标\n",
    "print(f\"RMSEc (校正均方根误差): {rmsec}\\nRMSEp (预测均方根误差): {rmsep}\\nRcal (校正集相关系数): {r_cal}\\nRval (验证集相关系数): {r_val}\\nRPD (相对预测偏差): {RPD}\")\n",
    "print(f\"Training time: {train_time} seconds\")\n",
    "print(f\"Testing time: {test_time} seconds\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "学习率 (Learning Rate): 可以尝试不同的学习率，或者使用学习率衰减策略。\n",
    "隐藏单元 (Hidden Units): 调整LSTM层中的隐藏单元数量。\n",
    "批量大小 (Batch Size): 尝试不同的批量大小以找到最优的训练速度和稳定性的平衡。\n",
    "滤波器数量 (Number of Filters): 调整卷积层中的滤波器数量。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:6 out of the last 19 calls to <function TensorFlowTrainer.make_predict_function.<locals>.one_step_on_data_distributed at 0x000002D607DA7A60> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.\n",
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 162ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 128, DR: 0.2, AF: relu, RMSE: 16.33623200632348\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 158ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 128, DR: 0.2, AF: elu, RMSE: 21.75903272607903\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 168ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 128, DR: 0.2, AF: selu, RMSE: 15.670815409314702\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m2s\u001b[0m 167ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 128, DR: 0.3, AF: relu, RMSE: 21.649820978580735\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 201ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 128, DR: 0.3, AF: elu, RMSE: 14.547415926100996\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 167ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 128, DR: 0.3, AF: selu, RMSE: 12.977758846936688\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 183ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 128, DR: 0.5, AF: relu, RMSE: 28.615323312511777\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m6s\u001b[0m 211ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 128, DR: 0.5, AF: elu, RMSE: 14.063654271316468\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 174ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 128, DR: 0.5, AF: selu, RMSE: 18.841695227831625\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 190ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 256, DR: 0.2, AF: relu, RMSE: 12.475258357488874\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 168ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 256, DR: 0.2, AF: elu, RMSE: 11.877158859104458\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 172ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 256, DR: 0.2, AF: selu, RMSE: 12.717354461508796\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 182ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 256, DR: 0.3, AF: relu, RMSE: 13.373825754102496\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 189ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 256, DR: 0.3, AF: elu, RMSE: 12.054318610408151\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 216ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 256, DR: 0.3, AF: selu, RMSE: 16.31255118017687\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 187ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 256, DR: 0.5, AF: relu, RMSE: 21.288775726811288\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 161ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 256, DR: 0.5, AF: elu, RMSE: 20.048718149374444\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 187ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 256, DR: 0.5, AF: selu, RMSE: 15.387618843429236\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 239ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 512, DR: 0.2, AF: relu, RMSE: 13.163217786326673\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 179ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 512, DR: 0.2, AF: elu, RMSE: 14.057054187392277\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 200ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 512, DR: 0.2, AF: selu, RMSE: 16.97489615116616\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 188ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 512, DR: 0.3, AF: relu, RMSE: 12.612510635990885\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 217ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 512, DR: 0.3, AF: elu, RMSE: 13.012851236021113\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 222ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 512, DR: 0.3, AF: selu, RMSE: 12.499069696186755\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 248ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 512, DR: 0.5, AF: relu, RMSE: 12.621262048118059\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m2s\u001b[0m 343ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 512, DR: 0.5, AF: elu, RMSE: 21.80647832905064\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m2s\u001b[0m 385ms/step\n",
      "LR: 0.001, HU: 64, BS: 10, F: 512, DR: 0.5, AF: selu, RMSE: 14.161246194146255\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 267ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 128, DR: 0.2, AF: relu, RMSE: 17.902285720016696\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 235ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 128, DR: 0.2, AF: elu, RMSE: 19.133833461677447\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 242ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 128, DR: 0.2, AF: selu, RMSE: 12.034237406745524\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 226ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 128, DR: 0.3, AF: relu, RMSE: 23.22448401048089\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 198ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 128, DR: 0.3, AF: elu, RMSE: 18.014653467719448\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 221ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 128, DR: 0.3, AF: selu, RMSE: 13.991196090071012\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 194ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 128, DR: 0.5, AF: relu, RMSE: 18.91048221038843\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 163ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 128, DR: 0.5, AF: elu, RMSE: 15.000864717804747\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 207ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 128, DR: 0.5, AF: selu, RMSE: 27.966561202653875\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 188ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 256, DR: 0.2, AF: relu, RMSE: 14.141008429673578\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 166ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 256, DR: 0.2, AF: elu, RMSE: 18.638762126441147\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 179ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 256, DR: 0.2, AF: selu, RMSE: 12.673124995032897\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 162ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 256, DR: 0.3, AF: relu, RMSE: 21.145669658416573\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 186ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 256, DR: 0.3, AF: elu, RMSE: 15.00527841908861\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 222ms/step\n",
      "LR: 0.001, HU: 64, BS: 32, F: 256, DR: 0.3, AF: selu, RMSE: 19.125127705622212\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\convolutional\\base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
      "  super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n",
      "C:\\Users\\30382\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\keras\\src\\layers\\activations\\leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use `negative_slope` instead.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import pandas as pd\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from scipy.stats import pearsonr\n",
    "import numpy as np\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Dense, Conv1D, LSTM, BatchNormalization, LeakyReLU, AveragePooling1D, Dropout\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\n",
    "\n",
    "# 读取数据\n",
    "file_path = 'F:\\\\研究\\\\番泻苷在线提取数据.xlsx'\n",
    "with pd.ExcelFile(file_path) as xls:\n",
    "    ir_data = pd.read_excel(xls, '红外谱图', index_col='编号\\波数')\n",
    "    targets = pd.read_excel(xls, '番泻苷含量')[['番泻苷B']]\n",
    "\n",
    "# 特征提取\n",
    "def extract_features(data):\n",
    "    pca = PCA(n_components=10)\n",
    "    pca_features = pca.fit_transform(data)\n",
    "    return pd.DataFrame(pca_features, columns=['PC' + str(i) for i in range(1, 11)])\n",
    "\n",
    "pca_features_df = extract_features(ir_data)\n",
    "\n",
    "# 索引对齐\n",
    "pca_features_df, targets = pca_features_df.align(targets, join='inner', axis=0)\n",
    "\n",
    "# Kennard-Stone算法实现省略，假设函数名为kennard_stone_selection\n",
    "def kennard_stone_selection(x_variables, k):\n",
    "    x_variables = np.array(x_variables)\n",
    "    original_x = x_variables\n",
    "    distance_to_average = ((x_variables - np.tile(x_variables.mean(axis=0), (x_variables.shape[0], 1))) ** 2).sum(\n",
    "        axis=1)\n",
    "    max_distance_sample_number = np.where(distance_to_average == np.max(distance_to_average))\n",
    "    max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "    selected_sample_numbers = list()\n",
    "    selected_sample_numbers.append(max_distance_sample_number)\n",
    "    remaining_sample_numbers = np.arange(0, x_variables.shape[0], 1)\n",
    "    x_variables = np.delete(x_variables, selected_sample_numbers, 0)\n",
    "    remaining_sample_numbers = np.delete(remaining_sample_numbers, selected_sample_numbers, 0)\n",
    "    for iteration in range(1, k):\n",
    "        selected_samples = original_x[selected_sample_numbers, :]\n",
    "        min_distance_to_selected_samples = list()\n",
    "        for min_distance_calculation_number in range(0, x_variables.shape[0]):\n",
    "            distance_to_selected_samples = ((selected_samples - np.tile(x_variables[min_distance_calculation_number, :],\n",
    "                                                                        (selected_samples.shape[0], 1))) ** 2).sum(\n",
    "                axis=1)\n",
    "            min_distance_to_selected_samples.append(np.min(distance_to_selected_samples))\n",
    "        max_distance_sample_number = np.where(\n",
    "            min_distance_to_selected_samples == np.max(min_distance_to_selected_samples))\n",
    "        max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "        selected_sample_numbers.append(remaining_sample_numbers[max_distance_sample_number])\n",
    "        x_variables = np.delete(x_variables, max_distance_sample_number, 0)\n",
    "        remaining_sample_numbers = np.delete(remaining_sample_numbers, max_distance_sample_number, 0)\n",
    "\n",
    "    return selected_sample_numbers, remaining_sample_numbers\n",
    "\n",
    "\n",
    "# 划分数据集\n",
    "train_indices, test_indices = kennard_stone_selection(pca_features_df.values, 336)\n",
    "X_train, X_test = pca_features_df.iloc[train_indices], pca_features_df.iloc[test_indices]\n",
    "y_train, y_test = targets.iloc[train_indices], targets.iloc[test_indices]\n",
    "\n",
    "# 调整数据形状\n",
    "X_train = np.expand_dims(X_train, axis=2)\n",
    "X_test = np.expand_dims(X_test, axis=2)\n",
    "\n",
    "# 定义超参数\n",
    "learning_rates = [1e-3, 1e-4, 1e-5]  # 尝试不同的学习率\n",
    "hidden_units = [64, 128, 256]  # 尝试不同的隐藏单元数量\n",
    "batch_sizes = [10, 32, 64]  # 尝试不同的批量大小\n",
    "filters = [128, 256, 512]  # 尝试不同的滤波器数量\n",
    "dropout_rates = [0.2, 0.3, 0.5]  # 尝试不同的Dropout比率\n",
    "activation_functions = ['relu', 'elu', 'selu']  # 尝试不同的激活函数\n",
    "\n",
    "# 定义一个函数来构建和训练模型，并返回性能指标\n",
    "def train_model(X_train, y_train, X_test, y_test, learning_rate, hidden_unit, batch_size, filters, dropout_rate, activation_function):\n",
    "    # 构建模型\n",
    "    model = Sequential([\n",
    "        Conv1D(filters=filters, kernel_size=3, activation=activation_function, input_shape=X_train.shape[1:]),\n",
    "        BatchNormalization(),\n",
    "        LeakyReLU(alpha=0.01),\n",
    "        AveragePooling1D(pool_size=2),\n",
    "        Dropout(dropout_rate),\n",
    "        Conv1D(filters=filters, kernel_size=3, activation=activation_function),\n",
    "        BatchNormalization(),\n",
    "        LeakyReLU(alpha=0.01),\n",
    "        AveragePooling1D(pool_size=2),\n",
    "        Dropout(dropout_rate),\n",
    "        LSTM(hidden_unit, return_sequences=True),\n",
    "        LSTM(hidden_unit),\n",
    "        Dense(1)\n",
    "    ])\n",
    "    optimizer = Adam(learning_rate=learning_rate)\n",
    "    model.compile(optimizer=optimizer, loss='mean_squared_error')\n",
    "    \n",
    "    # 训练模型\n",
    "    early_stopping = EarlyStopping(monitor='val_loss', patience=100, restore_best_weights=True)\n",
    "    history = model.fit(X_train, y_train, epochs=1000, batch_size=batch_size, validation_split=0.2, verbose=0, callbacks=[early_stopping])\n",
    "    \n",
    "    # 测试模型\n",
    "    y_pred = model.predict(X_test)\n",
    "    \n",
    "    # 计算性能指标\n",
    "    rmse = np.sqrt(mean_squared_error(y_test, y_pred))\n",
    "    \n",
    "    return rmse\n",
    "\n",
    "# 遍历超参数组合\n",
    "best_rmse = float('inf')\n",
    "best_params = {}\n",
    "for lr in learning_rates:\n",
    "    for hu in hidden_units:\n",
    "        for bs in batch_sizes:\n",
    "            for f in filters:\n",
    "                for dr in dropout_rates:\n",
    "                    for af in activation_functions:\n",
    "                        rmse = train_model(X_train, y_train, X_test, y_test, lr, hu, bs, f, dr, af)\n",
    "                        if rmse < best_rmse:\n",
    "                            best_rmse = rmse\n",
    "                            best_params = {'Learning Rate': lr, 'Hidden Units': hu, 'Batch Size': bs, 'Filters': f, 'Dropout Rate': dr, 'Activation Function': af}\n",
    "                        print(f\"LR: {lr}, HU: {hu}, BS: {bs}, F: {f}, DR: {dr}, AF: {af}, RMSE: {rmse}\")\n",
    "\n",
    "# 输出最佳超参数和对应的性能指标\n",
    "print(f\"最佳超参数: {best_params}\")\n",
    "print(f\"最佳RMSE: {best_rmse}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义超参数\n",
    "learning_rates = [1e-3, 1e-4, 1e-5]  # 尝试不同的学习率\n",
    "hidden_units = [64, 128, 256]  # 尝试不同的隐藏单元数量\n",
    "batch_sizes = [10, 32, 64]  # 尝试不同的批量大小\n",
    "filters = [128, 256, 512]  # 尝试不同的滤波器数量\n",
    "dropout_rates = [0.2, 0.3, 0.5]  # 尝试不同的Dropout比率\n",
    "activation_functions = ['relu', 'elu', 'selu']  # 尝试不同的激活函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import pandas as pd\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from scipy.stats import pearsonr\n",
    "import numpy as np\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Dense, Conv1D, LSTM, BatchNormalization, LeakyReLU, AveragePooling1D, Dropout\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\n",
    "\n",
    "# 读取数据\n",
    "file_path = 'F:\\\\研究\\\\番泻苷在线提取数据.xlsx'\n",
    "with pd.ExcelFile(file_path) as xls:\n",
    "    ir_data = pd.read_excel(xls, '红外谱图', index_col='编号\\波数')\n",
    "    targets = pd.read_excel(xls, '番泻苷含量')[['番泻苷B']]\n",
    "\n",
    "# 特征提取\n",
    "def extract_features(data):\n",
    "    pca = PCA(n_components=10)\n",
    "    pca_features = pca.fit_transform(data)\n",
    "    return pd.DataFrame(pca_features, columns=['PC' + str(i) for i in range(1, 11)])\n",
    "\n",
    "pca_features_df = extract_features(ir_data)\n",
    "\n",
    "# 索引对齐\n",
    "pca_features_df, targets = pca_features_df.align(targets, join='inner', axis=0)\n",
    "\n",
    "# Kennard-Stone算法实现省略，假设函数名为kennard_stone_selection\n",
    "def kennard_stone_selection(x_variables, k):\n",
    "    x_variables = np.array(x_variables)\n",
    "    original_x = x_variables\n",
    "    distance_to_average = ((x_variables - np.tile(x_variables.mean(axis=0), (x_variables.shape[0], 1))) ** 2).sum(\n",
    "        axis=1)\n",
    "    max_distance_sample_number = np.where(distance_to_average == np.max(distance_to_average))\n",
    "    max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "    selected_sample_numbers = list()\n",
    "    selected_sample_numbers.append(max_distance_sample_number)\n",
    "    remaining_sample_numbers = np.arange(0, x_variables.shape[0], 1)\n",
    "    x_variables = np.delete(x_variables, selected_sample_numbers, 0)\n",
    "    remaining_sample_numbers = np.delete(remaining_sample_numbers, selected_sample_numbers, 0)\n",
    "    for iteration in range(1, k):\n",
    "        selected_samples = original_x[selected_sample_numbers, :]\n",
    "        min_distance_to_selected_samples = list()\n",
    "        for min_distance_calculation_number in range(0, x_variables.shape[0]):\n",
    "            distance_to_selected_samples = ((selected_samples - np.tile(x_variables[min_distance_calculation_number, :],\n",
    "                                                                        (selected_samples.shape[0], 1))) ** 2).sum(\n",
    "                axis=1)\n",
    "            min_distance_to_selected_samples.append(np.min(distance_to_selected_samples))\n",
    "        max_distance_sample_number = np.where(\n",
    "            min_distance_to_selected_samples == np.max(min_distance_to_selected_samples))\n",
    "        max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "        selected_sample_numbers.append(remaining_sample_numbers[max_distance_sample_number])\n",
    "        x_variables = np.delete(x_variables, max_distance_sample_number, 0)\n",
    "        remaining_sample_numbers = np.delete(remaining_sample_numbers, max_distance_sample_number, 0)\n",
    "\n",
    "    return selected_sample_numbers, remaining_sample_numbers\n",
    "\n",
    "\n",
    "# 划分数据集\n",
    "train_indices, test_indices = kennard_stone_selection(pca_features_df.values, 336)\n",
    "X_train, X_test = pca_features_df.iloc[train_indices], pca_features_df.iloc[test_indices]\n",
    "y_train, y_test = targets.iloc[train_indices], targets.iloc[test_indices]\n",
    "\n",
    "# 调整数据形状\n",
    "X_train = np.expand_dims(X_train, axis=2)\n",
    "X_test = np.expand_dims(X_test, axis=2)\n",
    "\n",
    "# 定义超参数\n",
    "learning_rates = [1e-3]  # 尝试不同的学习率\n",
    "hidden_units = [64, 128, 256]  # 尝试不同的隐藏单元数量\n",
    "batch_sizes = [10, 32, 64]  # 尝试不同的批量大小\n",
    "filters = [128, 256, 512]  # 尝试不同的滤波器数量\n",
    "dropout_rates = [0.2]  # 尝试不同的Dropout比率\n",
    "activation_functions = ['relu', 'elu', 'selu','tanh']  # 尝试不同的激活函数\n",
    "\n",
    "# 定义一个函数来构建和训练模型，并返回性能指标\n",
    "def train_model(X_train, y_train, X_test, y_test, learning_rate, hidden_unit, batch_size, filters, dropout_rate, activation_function):\n",
    "    # 构建模型\n",
    "    model = Sequential([\n",
    "        Conv1D(filters=filters, kernel_size=3, activation=activation_function, input_shape=X_train.shape[1:]),\n",
    "        BatchNormalization(),\n",
    "        LeakyReLU(alpha=0.01),\n",
    "        AveragePooling1D(pool_size=2),\n",
    "        Dropout(dropout_rate),\n",
    "        Conv1D(filters=filters, kernel_size=3, activation=activation_function),\n",
    "        BatchNormalization(),\n",
    "        LeakyReLU(alpha=0.01),\n",
    "        AveragePooling1D(pool_size=2),\n",
    "        Dropout(dropout_rate),\n",
    "        LSTM(hidden_unit, return_sequences=True),\n",
    "        LSTM(hidden_unit),\n",
    "        Dense(1)\n",
    "    ])\n",
    "    optimizer = Adam(learning_rate=learning_rate)\n",
    "    model.compile(optimizer=optimizer, loss='mean_squared_error')\n",
    "    \n",
    "    # 训练模型\n",
    "    early_stopping = EarlyStopping(monitor='val_loss', patience=100, restore_best_weights=True)\n",
    "    history = model.fit(X_train, y_train, epochs=1000, batch_size=batch_size, validation_split=0.2, verbose=0, callbacks=[early_stopping])\n",
    "    \n",
    "    # 测试模型\n",
    "    y_pred = model.predict(X_test)\n",
    "    \n",
    "    # 计算性能指标\n",
    "    def calculate_metrics(y_true, y_pred):\n",
    "        rmse = np.sqrt(mean_squared_error(y_true, y_pred))\n",
    "        r = pearsonr(y_true.ravel(), y_pred.ravel())[0]\n",
    "        return rmse, r\n",
    "\n",
    "    rmsec, r_cal = calculate_metrics(y_train.values, model.predict(X_train))\n",
    "    rmsep, r_val = calculate_metrics(y_test.values, y_pred)\n",
    "    RPD = np.std(y_test.values) / rmsep\n",
    "    \n",
    "    return rmsec\n",
    "\n",
    "# 遍历超参数组合\n",
    "best_rmsec = float('inf')\n",
    "best_params = {}\n",
    "for lr in learning_rates:\n",
    "    for hu in hidden_units:\n",
    "        for bs in batch_sizes:\n",
    "            for f in filters:\n",
    "                for dr in dropout_rates:\n",
    "                    for af in activation_functions:\n",
    "                        rmsec = train_model(X_train, y_train, X_test, y_test, lr, hu, bs, f, dr, af)\n",
    "                        if rmsec < best_rmsec:\n",
    "                            best_rmsec = rmsec\n",
    "                            best_params = {'Learning Rate': lr, 'Hidden Units': hu, 'Batch Size': bs, 'Filters': f, 'Dropout Rate': dr, 'Activation Function': af}\n",
    "                        print(f\"LR: {lr}, HU: {hu}, BS: {bs}, F: {f}, DR: {dr}, AF: {af}, RMSEc: {rmsec}\")\n",
    "\n",
    "# 输出最佳超参数和对应的性能指标\n",
    "print(f\"最佳超参数: {best_params}\")\n",
    "print(f\"最佳RMSEc: {best_rmse}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
